package com.woniu.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.woniu.outlet.client.WorkingProcedures;
import com.woniu.outlet.dao.ProcessHandoverMapper;
import com.woniu.outlet.dao.ProgressMapper;
import com.woniu.outlet.po.MaterialApplication;
import com.woniu.outlet.po.ProcessHandover;
import com.woniu.outlet.po.ProcessHandoverExample;
import com.woniu.outlet.po.Progress;
import com.woniu.outlet.rabbitmq.EnergyRabbitmqRepo;
import com.woniu.outlet.rabbitmq.HandRabbitmqRepo;
import com.woniu.outlet.redis.ERPRedisRepo;
import com.woniu.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ProcessHandoverAdapter {
    @Autowired
    private ERPRedisRepo erpRedisRepo;
    @Autowired
    private ProcessHandoverMapper processHandoverMapper;
    @Autowired
    private HandRabbitmqRepo handRabbitmqRepo;
    @Autowired
    private WorkingProcedures userClient;
    @Autowired
    private EnergyRabbitmqRepo energyRabbitmqRepo;
    @Autowired
    private WorkingProcedures workingProcedures;

    ObjectMapper mapper = new ObjectMapper();
    CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, MaterialApplication.class);

    public void addProcessHandover(ProcessHandover processHandover){
        ResponseResult<Long> workerByCode = userClient.getWorkerByCode(processHandover.getHandoverName());
        processHandover.setHandoverPersonId(workerByCode.getData().intValue());
        erpRedisRepo.deleteList("processHandoverList");

        //查询上一道工序的报工时间,与当前工序的交接时间做减法,算出这个工序所用时间,将工序id与所用时间传给设备那边
        String processCode = workingProcedures.getSuperByCode(processHandover.getProcessCode()).getData();
        ProcessHandoverExample processHandoverExample = new ProcessHandoverExample();
        ProcessHandoverExample.Criteria criteria = processHandoverExample.createCriteria();
        criteria.andProcessCodeEqualTo(processCode);
        ProcessHandover SuperPro = processHandoverMapper.selectByExample(processHandoverExample).get(0);
        String spare2 = SuperPro.getSpare2();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        Date t1 = null;
        try {
             t1 = format.parse(spare2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long send = processHandover.getHandoverTime().getTime() - t1.getTime();
//        发消息队列给设备那边汇报能源使用情况
        energyRabbitmqRepo.sendAppMessage(processHandover.getProcessCode()+"-"+send);
//        energyRabbitmqRepo.sendAppMessage();
        processHandoverMapper.insertBy(processHandover);
        workingProcedures.addSystemLog("添加进度");
    }

    public List<ProcessHandover> getProcessHandoverList(String orderCode) throws JsonProcessingException {
        List<ProcessHandover> list = new ArrayList<>();
        String str = erpRedisRepo.getList( "processHandoverList");
        if(StringUtils.isEmpty(str)){
            list = processHandoverMapper.selectAll(orderCode);
            erpRedisRepo.addList(mapper.writeValueAsString(list),"applicationDetailList");
        }else{
            list = mapper.readValue(str,type);
        }
        workingProcedures.addSystemLog("获取工序列表");
        return list;
    }

    public void updateProcessHandover(ProcessHandover processHandover){
        erpRedisRepo.deleteList("processHandoverList");
        processHandoverMapper.updateByPrimaryKeySelective(processHandover);
        workingProcedures.addSystemLog("修改工序交接");
    }

    public void delProcessHandover(Integer id){
        erpRedisRepo.deleteList("processHandoverList");
        processHandoverMapper.deleteByPrimaryKey(id);
        workingProcedures.addSystemLog("根据id删除工序交接");
    }

    public void urgent (ProcessHandover processHandover) {
        handRabbitmqRepo.sendAppMessage(processHandover.getPartsCode()+"-"+processHandover.getPartsName());
        workingProcedures.addSystemLog("工序交接");
    }

    public List<ProcessHandover> getWorkReport (String orderCode) throws JsonProcessingException {
        List<ProcessHandover> list = new ArrayList<>();
        String str = erpRedisRepo.getList( "processHandoverList");
        if(StringUtils.isEmpty(str)){
            list = processHandoverMapper.selectWorkReport(orderCode);
            erpRedisRepo.addList(mapper.writeValueAsString(list),"applicationDetailList");
        }else{
            list = mapper.readValue(str,type);
        }
        return list;
    }

    public ProcessHandover getProcessHandoverById(Integer id) {
        return processHandoverMapper.selectByPrimaryKey(id);
    }
}
